package cn.t.repository.impl.order;

import cn.t.constants.ParamConstants;
import cn.t.converter.order.ReplenishmentRecordConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.order.ReplenishmentRecordDO;
import cn.t.persistence.entity.order.ReplenishmentRecordPO;
import cn.t.persistence.mapper.order.ReplenishmentRecordMapper;
import cn.t.repository.order.ReplenishmentRecordRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 后台补货记录表(ReplenishmentRecord)数据仓库
 *
 * @author t
 * @since 2024-05-24 10:18:11
 */
@Slf4j
@Repository
@AllArgsConstructor
public class ReplenishmentRecordRepositoryImpl extends RepositoryImpl<ReplenishmentRecordMapper, ReplenishmentRecordPO, ReplenishmentRecordConverter, ReplenishmentRecordDO> implements ReplenishmentRecordRepository {


    @Override
    public List<ReplenishmentRecordDO> findByModel(ReplenishmentRecordDO replenishmentRecordDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ?
                " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<ReplenishmentRecordPO> list = getWrapper(replenishmentRecordDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<ReplenishmentRecordPO> getWrapper(ReplenishmentRecordDO replenishmentRecordDO) {
        LambdaQueryChainWrapper<ReplenishmentRecordPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(replenishmentRecordDO.getDeviceId())) {
            wrapper.eq(ReplenishmentRecordPO::getDeviceId, replenishmentRecordDO.getDeviceId());
        }
        if (!ObjectUtils.isEmpty(replenishmentRecordDO.getCargowayId())) {
            wrapper.eq(ReplenishmentRecordPO::getCargowayId, replenishmentRecordDO.getCargowayId());
        }
        if (!ObjectUtils.isEmpty(replenishmentRecordDO.getCargowayNo())) {
            wrapper.eq(ReplenishmentRecordPO::getCargowayNo, replenishmentRecordDO.getCargowayNo());
        }
        if (!ObjectUtils.isEmpty(replenishmentRecordDO.getUserId())) {
            wrapper.eq(ReplenishmentRecordPO::getUserId, replenishmentRecordDO.getUserId());
        }
        if (!ObjectUtils.isEmpty(replenishmentRecordDO.getParticipantId())) {
            wrapper.eq(ReplenishmentRecordPO::getParticipantId, replenishmentRecordDO.getParticipantId());
        }
        if (!ObjectUtils.isEmpty(replenishmentRecordDO.getFirmId())) {
            wrapper.eq(ReplenishmentRecordPO::getFirmId, replenishmentRecordDO.getFirmId());
        }
        if (!ObjectUtils.isEmpty(replenishmentRecordDO.getSkuCode())) {
            wrapper.eq(ReplenishmentRecordPO::getSkuCode, replenishmentRecordDO.getSkuCode());
        }
        if (!ObjectUtils.isEmpty(replenishmentRecordDO.getSkuName())) {
            wrapper.eq(ReplenishmentRecordPO::getSkuName, replenishmentRecordDO.getSkuName());
        }
        if (!ObjectUtils.isEmpty(replenishmentRecordDO.getRsQty())) {
            wrapper.eq(ReplenishmentRecordPO::getRsQty, replenishmentRecordDO.getRsQty());
        }
        return wrapper;
    }

    @Override
    public long count(ReplenishmentRecordDO replenishmentRecordDO) {
        return getWrapper(replenishmentRecordDO).count();
    }

    @Override
    public boolean addBatch(List<ReplenishmentRecordDO> replenishmentRecordDOs) {
        List<ReplenishmentRecordPO> replenishmentDetailList = replenishmentRecordDOs.stream().map(converter::model2Po).collect(Collectors.toList());
        return saveBatch(replenishmentDetailList, ParamConstants.EXPORTPAGEMAXSIZE);
    }
}
